//
//  DiaryModel.swift
//  MyDiary2025
//
//  Created by 罗万能 on 2025/6/25.
//

import Foundation
import SwiftData
import CoreLocation

import SwiftUI

typealias DiaryModel = DiaryModelV2


@Model
class DiaryModelV1{
    @Attribute(.unique) var id: UUID
    var createDate: Date
    var lastedModifyDate: Date?
    var isSecret: Bool
    
    var title: String
    var content: String
    var images:[Data]
    var videos:[Data]
    var tag:[TagModel]
    var latitude: Double?
    var longitude: Double?
    var category: CategoryModel
    var weather: WeatherCondition
    @Transient var locationDescription: String = "无定位信息"
   @Transient var location: CLLocationCoordinate2D?
    
    init(id: UUID, createDate: Date, lastedModifyDate: Date? = nil, isSecret: Bool, title: String, content: String, images: [Data], videos: [Data], tag: [TagModel], latitude: Double? = nil, longitude: Double? = nil, category: CategoryModel,weather: WeatherCondition) {
        self.id = id
        self.createDate = createDate
        self.lastedModifyDate = lastedModifyDate
        self.isSecret = isSecret
        self.title = title
        self.content = content
        self.images = images
        self.videos = videos
        self.tag = tag
        self.latitude = latitude
        self.longitude = longitude
        self.category = category
        self.weather = weather
        
//       initLocation()
    }
    
}


@Model
class DiaryModelV2{
    @Attribute(.unique) var id: UUID
    var createDate: Date
    var lastedModifyDate: Date?
    var isSecret: Bool
    
    var title: String
    var content: String
    var images:[Data]
    var videos:[Data]
    var tag:[TagModel]
    var latitude: Double?
    var longitude: Double?
    var category: CategoryModel
    var weather: WeatherCondition
    var comment: [Comment]
    @Transient var locationDescription: String = "无定位信息"
   @Transient var location: CLLocationCoordinate2D?
    init(id: UUID, createDate: Date, lastedModifyDate: Date? = nil, isSecret: Bool, title: String, content: String, images: [Data], videos: [Data], tag: [TagModel], latitude: Double? = nil, longitude: Double? = nil, category: CategoryModel, weather: WeatherCondition, comment: [Comment]) {
        self.id = id
        self.createDate = createDate
        self.lastedModifyDate = lastedModifyDate
        self.isSecret = isSecret
        self.title = title
        self.content = content
        self.images = images
        self.videos = videos
        self.tag = tag
        self.latitude = latitude
        self.longitude = longitude
        self.category = category
        self.weather = weather
        self.comment = comment
        initLocation()
    }
    
}

extension DiaryModel{
    func initLocation(){
        if let lati = self.latitude, let longi = self.longitude{
            self.location = CLLocationCoordinate2D(latitude: lati, longitude: longi)
        }else{
            self.location = nil
            
        }
        getGeoLocation()
        
    }
    func getGeoLocation(){
        if let location = self.location{
            Task{@MainActor in
                let geo = CLGeocoder()
                let loc = CLLocation(latitude: location.latitude, longitude: location.longitude)
                let place = try await geo.reverseGeocodeLocation(loc)
                self.locationDescription = place.first?.name ?? "无定位信息"
            }
        }
    }
    
    static var debugData: DiaryModel = {
        var imgDatas:[Data] = []
        for i in 1...15 {
            if let img = UIImage(named: "\(i)"),let data = img.pngData(){
                imgDatas.append(data)
            }
        }
        let location = CLLocationCoordinate2D(latitude: 30.657413, longitude: 104.064773)
        if let filePath = Bundle.main.path(forResource: "video", ofType: "mp4"),  let fileData = FileManager.default.contents(atPath: filePath){
            return DiaryModel(id: UUID(), createDate: Date.now.addingTimeInterval(Double.random(in: ( -60 * 60 * 24 * 30 * 365 * 2) ..< -60)), isSecret: false, title: "测试标题", content: "处世三字经：真着急，假生气。热问题，冷处理。敢碰硬，不硬碰。不找事，不怕事。走直道，拐活弯。干累活，会歇息。办事情，分缓急。过去事，不后悔。眼前事，莫攀比。得到的，会失去。失去的，由它去。得志时，不忘形。失意时，不伤悲。尽全力，听天命。常宽己，好心情。多迷糊，长乐呵。", images: imgDatas, videos: [fileData], tag: TagModel.debugDatas,latitude: location.latitude, longitude:  location.longitude, category: CategoryModel.debugData,weather: WeatherCondition.random(), comment: Comment.debugDatas)
        }
        
       
        return DiaryModel(id: UUID(), createDate: Date.now.addingTimeInterval(Double.random(in: ( -60 * 60 * 24 * 30 * 365 * 2) ..< -60)), isSecret: false, title: "测试标题", content: "处世三字经：真着急，假生气。热问题，冷处理。敢碰硬，不硬碰。不找事，不怕事。走直道，拐活弯。干累活，会歇息。办事情，分缓急。过去事，不后悔。眼前事，莫攀比。得到的，会失去。失去的，由它去。得志时，不忘形。失意时，不伤悲。尽全力，听天命。常宽己，好心情。多迷糊，长乐呵。", images: imgDatas, videos: [], tag: TagModel.debugDatas, latitude: location.latitude, longitude:  location.longitude, category: CategoryModel.debugData,weather: WeatherCondition.random(), comment: Comment.debugDatas)
    }()
    
    static var debugDatas: [DiaryModel] = {
        var imgDatas:[Data] = []
        for i in 1...15 {
            if let img = UIImage(named: "\(i)"),let data = img.pngData(){
                imgDatas.append(data)
            }
        }
        let filePath = Bundle.main.path(forResource: "video", ofType: "mp4")
        var fileData: Data
        if filePath != nil{
            fileData = FileManager.default.contents(atPath: filePath!) ?? Data()
        }else{
            fileData = Data()
        }
         
        var debugDatas:[DiaryModel] = []
        for i in 0...20{
            var randomImage:[Data] = []
            let num  =  Int.random(in: 0..<15)
                for i in 0..<num{
                    randomImage.append(imgDatas[Int.random(in: 0..<imgDatas.count)])
                }
        
            let location = CLLocationCoordinate2D(latitude: 30.657413, longitude: 104.064773)
            let model = DiaryModel(id: UUID(), createDate: Date.now.addingTimeInterval(Double.random(in: ( -60 * 60 * 24 * 30 * 365 * 2) ..< -60)), isSecret: Bool.random(), title: "测试标题\(i)", content: "处世三字经：真着急，假生气。热问题，冷处理。敢碰硬，不硬碰。不找事，不怕事。走直道，拐活弯。干累活，会歇息。办事情，分缓急。过去事，不后悔。眼前事，莫攀比。得到的，会失去。失去的，由它去。得志时，不忘形。失意时，不伤悲。尽全力，听天命。常宽己，好心情。多迷糊，长乐呵。", images: randomImage, videos: Bool.random() ? [fileData] :[], tag: TagModel.debugDatas,latitude: location.latitude, longitude:  location.longitude, category: CategoryModel.debugDatas[Int.random(in: 0..<CategoryModel.debugDatas.count)],weather: WeatherCondition.random(), comment: Comment.debugDatas)
            debugDatas.append(model)
        }
        return debugDatas
    }()
}
